<template>
  <div>
    <!-- teleport -->
    <teleport to="body">
      <!-- 蒙版 -->
      <transition name="fade">
        <div
          v-show="modelValue"
          class="fixed top-0 left-0 z-40 w-screen h-screen bg-zinc-900/80"
          @click="isOpen = false"
        ></div>
      </transition>
      <!-- 内容 -->
      <transition name="popup-down-up">
        <div
          v-show="modelValue"
          v-bind="$attrs"
          class="fixed bottom-0 z-50 w-screen bg-white dark:bg-zinc-800"
        >
          <slot></slot>
        </div>
      </transition>
    </teleport>
  </div>
</template>

<script setup>
import { watch } from 'vue'
import { useScrollLock, useVModel } from '@vueuse/core'
const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true
  }
})
const emits = defineEmits(['update:modelValue']) // 无需主动触发

const isOpen = useVModel(props)

// 滚动锁定
const isLocked = useScrollLock(document.body)
watch(isOpen, (val) => {
  isLocked.value = val
})
</script>

<style lang="scss" scoped>
.fade-enter-active,
.fade-leave-active {
  transition: 250ms;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.popup-down-up-enter-active,
.popup-down-up-leave-active {
  transition: 250ms;
}

.popup-down-up-enter-from,
.popup-down-up-leave-to {
  transform: translateY(100%);
}
</style>
